package cn.virens.database.common.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.session.RowBounds;

import cn.hutool.core.collection.CollUtil;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

@tk.mybatis.mapper.annotation.RegisterMapper
public interface BaseMapper<T> extends Mapper<T> {
	public static final RowBounds ONE = new RowBounds(0, 1);

	public default T selectOne(Example example) {
		List<T> list = selectByExampleAndRowBounds(example, ONE);

		if (CollUtil.isNotEmpty(list)) {
			return list.get(0);
		} else {
			return null;
		}
	}

	@SelectProvider(type = ExampleJoinProvider.class, method = "dynamicSQL")
	T selectJoinOne(ExampleWrapper example);

	@SelectProvider(type = ExampleJoinProvider.class, method = "dynamicSQL")
	List<T> selectJoin(ExampleWrapper example);

	@SelectProvider(type = ExampleJoinProvider.class, method = "dynamicSQL")
	Map<String, Object> selectJoinMapOne(ExampleWrapper example);

	@SelectProvider(type = ExampleJoinProvider.class, method = "dynamicSQL")
	List<Map<String, Object>> selectJoinMap(ExampleWrapper example);

}
